package com.xxx.video.dynamic.activitys;

import android.os.Bundle;
import android.widget.EditText;
import android.widget.TextView;

import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.xxx.video.R;
import com.xxx.video.commen.activitys.BaseActivity;
import com.xxx.video.commen.views.recyclerviews.RefreshRecyclerView;
import com.xxx.video.dynamic.adapters.ChatListAdapter;
import com.xxx.video.dynamic.beans.ChatListBean;
import com.xxx.video.dynamic.presenters.ChatPresenter;
import com.xxx.video.dynamic.presenters.IChatView;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class ChatActivity extends BaseActivity<ChatPresenter> implements IChatView, RefreshRecyclerView.OnRecyclerViewRefreshOrLoadMoreListener {
    @BindView(R.id.rcv_chat_list)
    RefreshRecyclerView rcvChatList;
    @BindView(R.id.et_chat_bottom_comment)
    EditText etChatBottomComment;
    @BindView(R.id.tv_chat_bottom_comment_send)
    TextView tvChatBottomCommentSend;
    @BindView(R.id.cl_chat_bottom_comment)
    ConstraintLayout clChatBottomComment;
    /**
     * user id
     */
    private String mUserId;
    /**
     * page
     */
    private int page = 1;
    /**
     * user name
     */
    private String mUserName;
    private String mUserAvatar;

    private ChatListAdapter mChatListAdapter;

    @Override
    protected ChatPresenter createPresenter() {
        return new ChatPresenter(this, this);
    }

    @Override
    public void initToolbarData() {
        super.initToolbarData();
        showActionBar();
        setActionBarMarginTop(0);
        setIsShowActionbarLine(true);
        setActionBarTitle(R.string.title_chat_activity);
    }

    @Override
    public int getContentLayout() {
        return R.layout.activity_chat;
    }

    @Override
    public void initData(Bundle extras) {
        if (extras != null) {
            mUserId = extras.getString("Id");
            mUserName = extras.getString("userName");
            mUserAvatar = extras.getString("userAvatar");
        }
        showRootLayoutStatus(1);
        getPresenter().interactionList(mUserId, page);
    }

    @Override
    public void showRootLayoutStatus(int status) {
        showLayoutStatus(status);
    }

    @OnClick(R.id.tv_chat_bottom_comment_send)
    public void onViewClicked() {
        getPresenter().addInteraction(mUserId, mUserName);
    }

    @Override
    public String getContent() {
        return etChatBottomComment.getText().toString().trim();
    }

    @Override
    public void addContentSuccess(String content) {
        etChatBottomComment.setText("");
        if (mChatListAdapter != null) {
            ChatListBean.PageBean.ListBean listBean = new ChatListBean.PageBean.ListBean();
            listBean.fComment = content;
            listBean.fCommtenterId = "other";
            mChatListAdapter.addData(listBean);
        }
    }

    @Override
    public void initData2View(List<ChatListBean.PageBean.ListBean> list) {
        rcvChatList.resetState();
        if (mChatListAdapter == null) {
            rcvChatList.setLayoutManager(new LinearLayoutManager(this));
            mChatListAdapter = new ChatListAdapter(this);
            mChatListAdapter.setUserId(mUserId, mUserAvatar);
            rcvChatList.setAdapter(mChatListAdapter);
            rcvChatList.setOnRecyclerViewRefreshOrLoadMoreListener(this);
            rcvChatList.setLoadMoreEnabled(false);
        }
        if (page == 1) {
            mChatListAdapter.setData(list);
        } else {
            mChatListAdapter.addData(list, 0);
        }
    }

    @Override
    public void onRecyclerviewRefresh() {
        page++;
        getPresenter().interactionList(mUserId, page);
    }

    @Override
    public void onRecyclerviewLoadMore() {

    }
}
